<template>
  <el-dialog
    :title="!dataForm.workNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="工位编号" prop="workId">
      <el-input v-model="dataForm.workId" placeholder="工位编号"></el-input>
    </el-form-item>
    <el-form-item label="工位名称" prop="workName">
      <el-input v-model="dataForm.workName" placeholder="工位名称"></el-input>
    </el-form-item>
    <el-form-item label="工位位置左上角X坐标" prop="workPosX1">
      <el-input v-model="dataForm.workPosX1" placeholder="工位位置左上角X坐标"></el-input>
    </el-form-item>
    <el-form-item label="工位位置左上角Y坐标" prop="workPosY1">
      <el-input v-model="dataForm.workPosY1" placeholder="工位位置左上角Y坐标"></el-input>
    </el-form-item>
    <el-form-item label="工位位置左上角Z坐标" prop="workPosZ1">
      <el-input v-model="dataForm.workPosZ1" placeholder="工位位置左上角Z坐标"></el-input>
    </el-form-item>
    <el-form-item label="工位位置右下角X坐标" prop="workPosX2">
      <el-input v-model="dataForm.workPosX2" placeholder="工位位置右下角X坐标"></el-input>
    </el-form-item>
    <el-form-item label="工位位置右下角Y坐标" prop="workPosY2">
      <el-input v-model="dataForm.workPosY2" placeholder="工位位置右下角Y坐标"></el-input>
    </el-form-item>
    <el-form-item label="工位位置右下角Z坐标" prop="workPosZ2">
      <el-input v-model="dataForm.workPosZ2" placeholder="工位位置右下角Z坐标"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          workNo: 0,
          workId: '',
          workName: '',
          workPosX1: '',
          workPosY1: '',
          workPosZ1: '',
          workPosX2: '',
          workPosY2: '',
          workPosZ2: ''
        },
        dataRule: {
          workId: [
            { required: true, message: '工位编号不能为空', trigger: 'blur' }
          ],
          workName: [
            { required: true, message: '工位名称不能为空', trigger: 'blur' }
          ],
          workPosX1: [
            { required: true, message: '工位位置左上角X坐标不能为空', trigger: 'blur' }
          ],
          workPosY1: [
            { required: true, message: '工位位置左上角Y坐标不能为空', trigger: 'blur' }
          ],
          workPosZ1: [
            { required: true, message: '工位位置左上角Z坐标不能为空', trigger: 'blur' }
          ],
          workPosX2: [
            { required: true, message: '工位位置右下角X坐标不能为空', trigger: 'blur' }
          ],
          workPosY2: [
            { required: true, message: '工位位置右下角Y坐标不能为空', trigger: 'blur' }
          ],
          workPosZ2: [
            { required: true, message: '工位位置右下角Z坐标不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.workNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.workNo) {
            this.$http({
              url: this.$http.adornUrl(`/workpos/workpos/info/${this.dataForm.workNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.workId = data.workpos.workId
                this.dataForm.workName = data.workpos.workName
                this.dataForm.workPosX1 = data.workpos.workPosX1
                this.dataForm.workPosY1 = data.workpos.workPosY1
                this.dataForm.workPosZ1 = data.workpos.workPosZ1
                this.dataForm.workPosX2 = data.workpos.workPosX2
                this.dataForm.workPosY2 = data.workpos.workPosY2
                this.dataForm.workPosZ2 = data.workpos.workPosZ2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/workpos/workpos/${!this.dataForm.workNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'workNo': this.dataForm.workNo || undefined,
                'workId': this.dataForm.workId,
                'workName': this.dataForm.workName,
                'workPosX1': this.dataForm.workPosX1,
                'workPosY1': this.dataForm.workPosY1,
                'workPosZ1': this.dataForm.workPosZ1,
                'workPosX2': this.dataForm.workPosX2,
                'workPosY2': this.dataForm.workPosY2,
                'workPosZ2': this.dataForm.workPosZ2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
